#define _CRT_SECURE_NO_WARNINGS
#include"pile.h"
void Adjustdown(int* p, int n, int parent)
{
	int minchilen = parent * 2 + 1;
	while (minchilen < n)
	{
		if (minchilen + 1 < n && p[minchilen] > p[minchilen + 1])
		{
			minchilen++;
		}
		if (p[parent] > p[minchilen])
		{
			swap(&p[parent], &p[minchilen]);
			parent = minchilen;
			minchilen = parent * 2 + 1;
		}
		else
			break;
	}
}

void swap1(piledata* a, piledata* b)
{
	piledata tmp = *a;
	*a = *b;
	*b = tmp;
}
int main()
{
	/*int a[] = { 65,100,70,32,50,60 };*/
	/*int b[] = {15,18,19,25,28,34,65,49,27,37}; */
	pile pi;
	initial(&pi);
	/*for (int i = 0; i < sizeof(b) / sizeof(int); i++)
	{
		pushpile(&pi, b[i]);
	}
	while (!emptypile(&pi))
	{
		printf("%d ", gettop(&pi));
		poppile(&pi);
	}*/
	int a[] = {15,1,19,25,8,34,65,4,27,7};
	int n = sizeof(a)/sizeof(a[0]);
	//printf("%d", n);
	for (int i = (n - 1 - 1) / 2;i>=0; i--)
	{
		Adjustdown(a,n,i);
	}
	int i = 1;
	while (i < n)
	{
		swap1(&a[0], &a[n - i]);
		Adjustdown(a, n - i, 0);
		i++;
	}
	return 0;
}
